package world.snowcrystal.datasource;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import world.snowcrystal.model.dto.post.PostQueryRequest;
import world.snowcrystal.model.entity.Post;
import world.snowcrystal.model.enums.SearchTypeEnum;
import world.snowcrystal.model.vo.PostVO;
import world.snowcrystal.service.PostService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 基于数据库的文章数据源
 *
 *  @author tianqing
 *  
 */
@Service
@Slf4j
public class EsPostDataSource implements DataSource<PostVO> {

    @Resource
    private PostService postService;

    @Override
    public Page<PostVO> doSearch(String searchText, long pageNum, long pageSize,HttpServletRequest request) {
        PostQueryRequest postQueryRequest = new PostQueryRequest();
        postQueryRequest.setSearchText(searchText);
        postQueryRequest.setCurrent(pageNum);
        postQueryRequest.setPageSize(pageSize);
        Page<Post> postPage = postService.searchFromEs(postQueryRequest);
        return postService.getPostVOPage(postPage, request);
    }
    @Override
    public boolean support(String dataType){
        return SearchTypeEnum.POST.getValue().equals(dataType);
    }
}




